package util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

@Component
public class PropertiesUtils {

    private static String profilepath = "";


    private static Properties props = new Properties();


    private static String env = System.getProperty("spring.profiles.active", "pro");


    public PropertiesUtils(String path) {
        setProfilepath(path);
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(path));
            props.load(is);
        } catch (Exception e) {
            return;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    System.out.println("PropertiesUtils-close-" + e);
                }
            }
        }


    }

    public String getKeyValue(String key) {
        return props.getProperty(key);
    }

    public Properties getAllProperties() {
        return props;
    }

    /**
     * @param keyname
     * @param keyvalue
     * @param isAppend ：是否追加
     */
    public void writeProperties(String keyname, String keyvalue, boolean isAppend) {
        OutputStream fos = null;
        try {
            File file = new File(getProfilepath());
            fos = new FileOutputStream(file);
            if (isAppend) {
                System.out.println(props);
                String v = props.get(keyname).toString();
                keyvalue = v + "," + keyvalue;
            }
            props.setProperty(keyname, keyvalue);
            props.store(fos, "Update '" + keyname + "' value");
        } catch (IOException e) {
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }


            }
        }
    }


    public void removeProperties(String keyname) {
        OutputStream fos = null;
        Properties properties = new Properties();
        try {

            File file = new File(getProfilepath());
            fos = new FileOutputStream(file);
            Enumeration enumeration = props.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                if (key.equals(keyname)) {
                    continue;
                }
                properties.setProperty(key, props.getProperty(keyname));
            }
            properties.store(fos, "Update '" + keyname + "' value");
            writeProperties(keyname, "", false);
        } catch (IOException e) {
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public void saveProperties(Map<String, Object> params) {
        try {
            Properties properties = new Properties();

            Properties p = new Properties();
            File file = new File(getProfilepath());
            p.load(new FileInputStream(file));
            Set<String> pSet = p.stringPropertyNames();
            Iterator i = pSet.iterator();
            for (String key : params.keySet()) {
                properties.setProperty(key, params.get(key).toString());
            }
//            properties.setProperty("usa", "美国");
//            properties.setProperty("hk", "香港");
//            properties.setProperty("japan", "日本");

            //添加
            properties.store(new FileOutputStream(file), properties.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProfilepath() {
        return profilepath;
    }

    public static void setProfilepath(String profilepath) {
        PropertiesUtils.profilepath = profilepath;
    }
}
